package com.xcc.shopanim.olda;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xcc.shopanim.BaseFragment;
import com.xcc.shopanim.OtherFragment;
import com.xcc.shopanim.R;
import com.xcc.shopanim.SimpleBaseActivity;
import com.xcc.shopanim.utils.NotchUtils;
import com.xcc.shopanim.utils.QMUIStatusBarHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends SimpleBaseActivity implements BaseFragment.OnFragmentCallBack {
    @BindView(R.id.tab3)
    View tab3;
    @BindView(R.id.layoutRoot)
    View layoutRoot;
    private int notchHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBar();
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        fragments = new BaseFragment[]{
                Main2Fragment.newInstance(),
                OtherFragment.newInstance(true),
                OtherFragment.newInstance()};

        removeFragment(savedInstanceState);
        layoutRoot.postDelayed(() -> {
            if (isDestroy) return;
            if (isF) {
                isF = false;
                asynRun();
            }
        }, 500);
    }

    protected void asynRun() {
        QMUIStatusBarHelper.setStatusBarDarkMode(getActivity());//白色
        /*
         *SDK 19 以下版本不能开启渗透式，所以不获取notchHeight
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            notchHeight = NotchUtils.getNotchHeight(getActivity());
        }
        onViewClicked(tab3);
        /*
         *Y97卸载安装，第一次启动不显示layoutFrame，
         * 这样设置一下就可以了，(@﹏@)~
         */
        View view = findViewById(R.id.layoutFrame);
        view.setBackgroundColor(Color.TRANSPARENT);
        //设置底部颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(0xFF1B1B1B);
        }
    }

    @OnClick({R.id.tab2, R.id.tab3, R.id.tab5})
    public void onViewClicked(View view) {
        if (view == null) return;
        if (view == oldView) return;
        if (oldView != null) oldView.setSelected(false);
        oldView = view;
        oldView.setSelected(true);
        switch (view.getId()) {
            case R.id.tab3:
                gotoFragment(fragments[0]);
                break;
            case R.id.tab2:
                gotoFragment(fragments[1]);
                break;
            case R.id.tab5:
                gotoFragment(fragments[2]);
                break;
        }
    }

    private View oldView;
    private BaseFragment[] fragments;
    public BaseFragment oldFragment;

    private void gotoFragment(BaseFragment fragment) {
        if (fragment == null) return;
        if (fragment == oldFragment) return;
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.layoutFrame, fragment);
        }
        if (oldFragment != null) transaction.hide(oldFragment);
        this.oldFragment = fragment;
        transaction.commitAllowingStateLoss();
    }

    @Override
    public Object onFragmentCallBack(BaseFragment fragment, int type, Object object) {
        Object obj = null;
        switch (type) {
            case 3://获取状态栏高度
                obj = notchHeight;
                break;
        }
        return obj;
    }
}
